package service;

import common.Exception.Results;
import dto.req.AdminRegisterReqDTO;
import dto.resp.AdminLoginRespDTO;
import dto.resp.AdminQueryWaitViewArticleRespDTO;
import entity.AdminDO;
import entity.AuthorDO;
import service.impl.AdminServiceImpl;
import service.impl.ArticleServiceImpl;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class AdminService implements AdminServiceImpl {

    private static ArticleService articleService;
    private static final String tableName = " admin ";

    public AdminService() {
        articleService = new ArticleService();
    }

    @Override
    public Results register(AdminRegisterReqDTO admin) {
        Results results = new Results();
        if(admin.getUsername() == null || admin.getPassword() == null){
            return results.fail("管理员名或密码不能为空");
        }
        if(admin.getUsername().length() < 6 || admin.getPassword().length() < 6){
            return results.fail("管理员名或密码长度不能小于6");
        }
        //进行插入操作，可能会导致插入失败
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = JDBCUtils.getConnection();
            //Statement statement = connection.createStatement();
            String sql = "INSERT INTO " + tableName + "(username, password,name) VALUES (?, ?,?)";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, admin.getUsername());
            pstmt.setString(2, admin.getPassword());
            pstmt.setString(3, admin.getName());
            // 执行SQL语句
            pstmt.executeUpdate();
            return results.success("注册成功！");
        } catch (Exception e) {
            return results.fail("注册失败，用户已存在");
        }finally {
            JDBCUtils.closeResource(connection,pstmt);
        }
    }

    @Override
    public Results<AdminLoginRespDTO> login(String username, String password) {
        Results<AdminLoginRespDTO> results = new Results<>();
        if(username == null || password == null){
            return results.fail("管理员名或密码不能为空");
        }
        if(username.length() < 6 || password.length() < 6){
            return results.fail("管理员名或密码长度不能小于6");
        }

        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT name FROM " + tableName+ " WHERE username = ? AND password = ?" ;
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);

            // 执行SQL语句
            rs = pstmt.executeQuery();
            if(!rs.next()){
                //用户不存在
                return results.fail("查询的用户不存在");
            }
            AdminLoginRespDTO adminLoginRespDTO = new AdminLoginRespDTO();
            adminLoginRespDTO.setName(rs.getString(1));
            return results.success(adminLoginRespDTO,"查询成功！");
        } catch (Exception e) {
            return results.fail("登录异常： " + e.getMessage());
        }finally {
            JDBCUtils.closeResource(connection,pstmt,rs);
        }

    }

    @Override
    public Results<ArrayList<AdminQueryWaitViewArticleRespDTO>> queryWaitViewArticleAll(Integer idx) {
        return articleService.queryWaitViewArticleAll(idx);
    }

    @Override
    public Results viewArticle(Integer articleId) {
        return articleService.viewArticle(articleId);
    }

    @Override
    public Results deleteArticle(Integer articleId) {
        if(articleId == null){
            Results results = new Results();
            return results.fail("文章不存在");
        }
        return articleService.deleteArticleByAdmin(articleId);
    }

}
